package mark.tom.friend.base.util;

import android.annotation.SuppressLint;
import android.view.View;

import com.jakewharton.rxbinding2.view.RxView;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.concurrent.TimeUnit;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class RxViewUtil {

    @SuppressWarnings("unchecked")
    @SuppressLint("CheckResult")
    public static void bindViewClickListener(final View view, LifecycleTransformer lifecycleTransformer, Consumer<View> consumer) {
        RxView.clicks(view)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .compose(lifecycleTransformer)
                .map(new Function<Object, View>() {
                    @Override
                    public View apply(@NonNull Object o) throws Exception {
                        return view;
                    }
                })
                .subscribe(consumer);
    }
}
